from sqlalchemy import *
from pylons.database import session_context
from sqlalchemy.ext.assignmapper import assign_mapper

metadata = MetaData()

user_table = Table('users', metadata,
                Column('id', Integer, primary_key=True),
                Column('userName', String(40), nullable=False),
                Column('firstName', String(40)),
                Column('lastName', String(40)),
                Column('password', String(10), nullable=False),
                Column('active', Boolean)
            )
run_table = Table('run', metadata,
                Column('id', Integer, primary_key=True),
                Column('user_id', Integer, ForeignKey('users.id')),
                Column('distance', Float(2), nullable=False),
                Column('date', Date)
            )
weight_table = Table('weight', metadata,
                Column('id', Integer, primary_key=True),
                Column('user_id', Integer, ForeignKey('users.id')),
                Column('weight', Float(2), nullable=False),
                Column('date', Date)
            )

class User(object):
    """Mapper for the user table """
    def __init__(self, userName, password, firstName='', lastName='', active=0):
        self.userName = userName
        self.password = password
        self.active = active
    def __repr__(self):
        return self.userName

class Run(object):
    """Mapper for the run table """
    def __init__(self, distance):
        self.distance = distance
    def __repr__(self):
        return self.distance

class Weight(object):
    """ Mapper for weight table """
    def __init__(self, weignt):
        self.weight = weight
    def __repr__(self):
        return self.weight

# Mappers
assign_mapper(session_context, Run, run_table)
assign_mapper(session_context, Weight, weight_table)
assign_mapper(session_context, User, user_table,
                       properties = {'runs': relation(Run), 'weights': relation(Weight)})
